package model;

import java.util.List;

/**
 *  Email class representation which is a holder for the email information
 */
public class Email implements EmailImp
{
	 /**
	  * 
	  */
	 private static final long serialVersionUID = -5522071496739700790L;
	
	 private String sender;
     private List<String> receiver;
	 private String subject;
	 private String message;
	 private String time;
     private String persistencePath = "";  
     
    
     /**
      * Default constructor
      */
     public Email()
     {
         
     } 
     
	/**
	 * 
	 * This is a method used to update the subject of the email.
	 * @param subject parameter takes the updated email subject as a string.
	 */
	public void updateSubject(String subject)
	{
	    this.subject = subject;
	}
	
	/**
	 * 
	 * This is a method used to update the message body of the email.
	 * @param message takes the updated email body as a single string.
	 */
	public void updateMessage(String message)
    {
        this.message = message;
    }
	
	/**
	 * 
	 * This is a method used to update the time of arrival of the email.
	 * @param time takes the updated time as a string.
	 */
	public void updateTime(String time)
	{
	    this.time = time;
	}
	
	/**
	 * This is an alternative constructor for the Email class, made using the builder pattern.
	 * The static class 'Builder' constructs a temporary 'Builder' object taking parameters for the email object and passes on to the Email object. 
	 * @param builder is the builder object which passes on the parameters to the Email object.
	 */
	private Email(Builder builder)
    {
		this.sender = builder.sender;
		this.receiver = builder.receiver;
		this.subject = builder.subject;
		this.message = builder.message;
		this.time = builder.time;
    }
	
	/**
	 * 
	 * @return Returns the email sender as a String.
	 */
	public String getSender()
    {
        return this.sender;
    }

	/**
	 * @return Returns the list of email recipients as a List of String objects.
	 */
    public List<String> getReceiver()
    {
        return this.receiver;
    }

	/**
	 * @return Returns the subject of the email as a String.
	 */
    public String getSubject()
    {
        return this.subject;
    }

    /**
	 * @return Returns the message or body content of the email as a String.
	 */
    public String getMessage()
    {
        return this.message;
    }
    
    /**
	 * @return Returns the time of the email received as a String object.
	 */
    public String getTime()
    {
        return this.time;
    }
    
	/**
	 * Sets the file path where email objects should be stored locally, this information is used only to persist but not saved in the file
	 * @param persistencePath is the path passed as a String. 
	 */
    public void setPersistencePath(String persistencePath)
    {
        this.persistencePath  = persistencePath;
    }
    

    /**
	 * @return Returns the persistence path reference
	 */
    public String getPersistencePath()
    {
        return this.persistencePath;
    }
    
    /**
     * @return returns a String with fields separated by the character ~, the values are in following order: Sender, Receivers, Subject, Message, Time.
     */
    public String asString()
    {
    	String stringReceivers="";
    	
    	for(String a_receiver:receiver)
    		stringReceivers += "~" + a_receiver;
    	
    	return this.sender + "~" +
			   stringReceivers +"~" +
			   this.subject +"~"+
			   this.message +"~" +
			   this.time;
    }
    
    /**
     * 
     * The builder pattern has been used to construct 'email' objects easily, helps to add more attributes
     * The static class Builder takes parameters for Email objects. 
     */
	public static class Builder 
	{
		private String sender;
	    private List<String> receiver;
		private String subject;
		private String message;
		private String time;

		/**
		 * set sender
		 * @param sender sender of the email
		 * @return return the Builder (builder pattern)
		 */
	    public Builder setSender(String sender)
	    {
	    	this.sender = sender;
	    	return this;
	    }
	    
	    /**
	     * set receiver(s)
         * @param receiver list of receivers
	     * @return return the Builder (builder pattern)
	     */
	    public Builder setReceiver(List<String> receiver)
	    {
	    	this.receiver = receiver;
	    	return this;
	    }
	    
	    /**
         * set subject
	     * @param subject subject of the email
	     * @return return the Builder (builder pattern)
	     */
	    public Builder setSubject(String subject)
	    {
	    	this.subject = subject;
	    	return this;
	    }
	    
	    /**
	     * set message
	     * @param message message of the email
	     * @return return the Builder (builder pattern)
	     */
	    public Builder setMessage(String message)
	    {
	    	this.message = message;
	    	return this;
	    }
	    
	    /**
	     * set time of creation
	     * @param time time of email reception
         * @return return the Builder (builder pattern)
	     */
	    public Builder setTimeOfCreation(String time)
	    {
	    	this.time = time;
	    	return this;
	    }
	    
	    /**
	     * @return to build the Email object.
	     */
	    public Email build()
	    {
	    	return new Email(this);
	    }    	
	}  
}
